//
//  CourseingDetailTableViewController.swift
//  约健
//
//  Created by tyh on 2017/10/10.
//  Copyright © 2017年 tyh. All rights reserved.
//

import UIKit

class CourseingDetailTableViewController: UITableViewController {
    var Course:CourseModel!
    var turebarbtnItem:UIBarButtonItem!
    var rightBtn:UIButton = UIButton.init()
    var videosList = NSArray()
    var array = NSMutableArray()
    override func viewDidLoad() {
        super.viewDidLoad()
        automaticallyAdjustsScrollViewInsets = false
        self.title = Course.train_name as String
        rightBtn.sizeToFit()
        rightBtn.addTarget(self, action: #selector(Ifcollect), for: UIControlEvents.touchUpInside)
        turebarbtnItem = UIBarButtonItem.init(customView: rightBtn)
        self.navigationItem.rightBarButtonItem = turebarbtnItem
        self.rightBtn.setImage(UIImage.init(named: "buzai.png"), for: .normal)
        CreatFootView()
        getvideo()
     


        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }
/*
    override func viewWillAppear(_ animated: Bool) {
        getvideo()
    }*/
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 3
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 1
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var basecell = UITableViewCell()
        if indexPath.section == 0{
        let cell = DetailHeadTableViewCell.creatCell(tableView)
        let imageStr = "http://172.24.10.175/workout/Uploads/" + (Course.thumb! as String)
        let imageUrl = URL(string: imageStr)
        let data = try! Data(contentsOf: imageUrl!)
        let image = UIImage(data: data)
        cell.Pic.image = image
        cell.Address.text = Course.body_name as String
         basecell = cell
        }
        else if indexPath.section == 2{
    
            if videosList.count != 0{
                let cell:CourseVideoTableViewCell = tableView.dequeueReusableCell(withIdentifier: "videocell", for: indexPath) as! CourseVideoTableViewCell
                
                
            cell.number = videosList.count
            cell.TT.text = "相关视频"
            for i in 0..<videosList.count{
                let model = videosList[i] as! VideoModel
                let imageStr = "http://172.24.10.175/workout/Uploads/" + (model.thumb! as String)
                let imageUrl = URL(string: imageStr)
                let data = try! Data(contentsOf: imageUrl!)
                let image = UIImage(data: data)
                (cell.imageViews[i] as! SingleView).pic.image = image
                (cell.imageViews[i] as! SingleView).Tittle.text = model.tvideo_name! as String
                (cell.imageViews[i] as! SingleView).Btn.tag = i
                (cell.imageViews[i] as! SingleView).Btn.addTarget(self, action: #selector(PlayVideo), for: .touchUpInside)
            }
                   basecell = cell
            }
            else{
                let cell = NoDataTableViewCell.creatCell(tableView)
                
                cell.JieShiText.text = "暂无相关视频！！"
                cell.Pic.image = UIImage(named: "没有数据.png")
                basecell = cell
                

            }
        }else{
            let cell:TextTableViewCell = tableView.dequeueReusableCell(withIdentifier: "textcell", for: indexPath) as! TextTableViewCell
            cell.TextName.text = "课程简介"
            cell.TextContent.text = Course.introduction as String
        }

        basecell.selectionStyle = UITableViewCellSelectionStyle.none
        return basecell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0{
        return height * 0.3
        }else if indexPath.section == 2{
            return height * 0.2
        }else {
            return height * 0.1
        }
    
    }
    
    func Ifcollect(){
        
    }
    func getvideo(){
        let parameters = ["mod":"training","SessionID":"2ul9sj8vlfofj122e1s98048k2","id":Course.id,"map":"tvideo","related":1] as NSDictionary
        NetWorkMngTool.sharedNetworkMngtool().Network_GetCourseVideoList(parameters, view: self.view){(videoList) in
            self.videosList = videoList
            self.tableView.reloadData()
        }
       
    }
    
    func PlayVideo(sender:UIButton){
        let model = videosList[sender.tag] as! VideoModel
        print("路径：http://172.24.10.175/workout/Uploads/tvideo/video/\(model.url!)")
        
    }
    
    func CreatFootView(){
        let footerView:UIView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: 60))
        let footButton:UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: width, height: 60))
        footButton.setTitle("下载课程", for: .normal)
        footButton.setTitleColor(UIColor.white, for: .normal)
        footButton.backgroundColor = UIColor.orange
        footButton.addTarget(self, action: #selector(XiaZai), for: .touchUpInside)
        footerView.addSubview(footButton)
        tableView.tableFooterView = footerView
        
    }
    func XiaZai(){
        
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        return 10
        
    }
    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
